<?php
namespace app\admin\controller;

use think\Controller;
use app\common\model\Music as Musics;

class Music extends Common {
	private $cModel;
	//当前控制器关联模型

	public function _initialize() {
		parent::_initialize();
		$this->cModel = new Musics;
		//别名：避免与控制名冲突
	}

//	public function index() {
//		if(request()->isPost()){
//			$type = input('type');
//			$where = ['type'=> $type];
//			$list_data = $this->cModel->where($where)->order('pid ASC,orderby ASC')->select();
//			$treeClass = new \expand\Tree();
//			$list = $treeClass->create($list_data);
//			return ajaxReturn('获取成功','',1,$list);
//		}else{
//			$where = [];
//			$type = input('type');
//			if ($type) {
//				$where = ['type' => $type];
//			}
//			$data = $this->cModel->where($where)->order('pid ASC, type ASC,orderby ASC')->select();
//			$treeClass = new \expand\Tree();
//			$dataList = $treeClass->create($data);
//			$this->assign('fenlei', $this->cModel->fenLei);
//			$this->assign('dataList', $dataList);
//
//			return $this->fetch();
//		}
//
//	}

	public function create() {//添加
		if (request()->isPost()) {
			$data = input('post.');
			$result = $this->cModel->validate(C_NAME . '.add')->allowField(true)->save($data);
			if ($result) {
				return ajaxReturn(lang('action_success'), url('Config/music'));
			} else {
				return ajaxReturn($this->cModel->getError());
			}
		} else {
//			$this->assign('fenlei', $this->cModel->fenLei);
			return $this->fetch('edit');
		}
	}

	public function edit($id) {//编辑
		if (request()->isPost()) {
			$data = input('post.');
            if (count($data) == 2){
                foreach ($data as $k =>$v){
                    $fv = $k!='id' ? $k : '';
                }
                $result = $this->cModel->validate(C_NAME.'.'.$fv)->allowField(true)->save($data, $data['id']);
            }else{
                $result = $this->cModel->validate(C_NAME.'.edit')->allowField(true)->save($data, $data['id']);
            }
			if ($result) {
				return ajaxReturn(lang('action_success'), url('Config/music'));
			} else {
				return ajaxReturn($this->cModel->getError());
			}
		} else {
			$data = $this->cModel->get($id);
			$this->assign('data', $data);
			return $this->fetch();
		}

	}

	public function edits() {//更新字段
//		$data = input('post.');
//      foreach ($data as $k =>$v){
//          $fv = $k!='id' ? $k : '';
//      }
//		$save = $this->cModel->validate(C_NAME.'.'.$fv)->allowField(true)->save($data,$data['id']);
//		if ($save) {
//			return ajaxReturn('更新成功', url('index'), 1);
//		} else {
//			return ajaxReturn($this->cModel->getError());
//		}

	}

	public function delete() {//删除
		if (request()->isPost()) {
			$id = input('id');
			if (isset($id) && !empty($id)) {
				$id_arr = explode(',', $id);
				$where = ['id' => ['in', $id_arr] ];
				$result = $this->cModel->where($where)->delete();
				if ($result) {
					return ajaxReturn(lang('action_success'), url('Config/music'));
				} else {
					return ajaxReturn($this->cModel->getError());
				}
			}
		}
	}

}
